/*
X2Play
Copyright (C) 2008  Jitu Das

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "x2play.h"
#include "../globalshortcut/globalshortcutmanager.h"

#ifndef X2PLAY_CPP
#define X2PLAY_CPP

X2Play::X2Play(QApplication *app, QWidget *parent) : QWidget(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
{
	this->app = app;
	conn = new XMMS2Conn("X2Play");
	setWindowOpacity(0.85);
	
	window = QPixmap(":images/window.png");
	
	// set window properties
	setFixedSize(400, 507);
	setMask(window.mask());
	QDesktopWidget *qdw = app->desktop();
	int left = (qdw->availableGeometry().width()-width())/2;
	int top = (qdw->availableGeometry().height()-height())/2;
	setGeometry(left, top, width(), height());
	setWindowTitle("X2Play");
	
	// add components
	searchBox = new SearchLineEdit(this);
	searchBox->setGeometry(70, 120, 260, 20);
	connect(searchBox, SIGNAL(textChanged(const QString&)), this, SLOT(searchTextChanged(const QString&)));
	connect(searchBox, SIGNAL(returnPressed()), this, SLOT(playClicked()));
	
	resultBox = new QTreeWidget(this);
	resultBox->setGeometry(50, 145, 300, 220);
	QStringList resultBoxHeaders;
	resultBoxHeaders << "Title" << "Artist" << "Album";// << "Id" << "Score";
	resultBox->setHeaderLabels(resultBoxHeaders);
	resultBox->setRootIsDecorated(false);
	connect(resultBox, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(playClicked()));
	connect(resultBox, SIGNAL(returnPressed()), this, SLOT(playClicked()));
	
	qRegisterMetaType< QList<QStringList> >("QList<QStringList>&");
	connect(conn, SIGNAL(updatedMatches(QList<QStringList>&)), this, SLOT(updateMatches(QList<QStringList>&)));
	
	btnPlay = new QPushButton("Play", this);
	btnPlay->setGeometry(70, 370, 260, 20);
	connect(btnPlay, SIGNAL(clicked()), this, SLOT(playClicked()));
	btnQueue = new QPushButton("Play Next", this);
	btnQueue->setGeometry(105, 395, 190, 20);
	connect(btnQueue, SIGNAL(clicked()), this, SLOT(queueClicked()));
	btnCancel = new QPushButton("Cancel", this);
	btnCancel->setGeometry(160, 420, 80, 20);
	connect(btnCancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
	
	// global shortcuts
	QKeySequence toggleSC = QKeySequence("Meta+F2");
	GlobalShortcutManager::connect(toggleSC,this,SLOT(toggle()));
	
	// shortcuts
	QAction *quitAction = new QAction("Quit", this);
	quitAction->setShortcut(tr("Ctrl+Q"));
	connect(quitAction, SIGNAL(triggered()), app, SLOT(quit()));
	addAction(quitAction);
	
	QAction *hideAction = new QAction("Hide", this);
	hideAction->setShortcut(tr("Esc"));
	connect(hideAction, SIGNAL(triggered()), this, SLOT(cancelClicked()));
	addAction(hideAction);

	/* replaced by returnPressed
	QAction *playAction = new QAction("Play", this);
	playAction->setShortcut(tr("Return"));
	connect(playAction, SIGNAL(triggered()), this, SLOT(playClicked()));
	addAction(playAction);
	*/

	QAction *queueAction = new QAction("Queue", this);
	queueAction->setShortcut(tr("Shift+Return"));
	connect(queueAction, SIGNAL(triggered()), this, SLOT(queueClicked()));
	addAction(queueAction);
}

void X2Play::toggle() {
	if (isVisible()) hide();
	else {
		searchBox->QLineEdit::clear();
		searchBox->setFocus();
		show();
	}
}

void X2Play::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void X2Play::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
        move(event->globalPos() - dragPosition);
        event->accept();
    }
}

void X2Play::paintEvent(QPaintEvent *)
{
	QPainter painter(this);

	painter.setRenderHint(QPainter::Antialiasing);
	painter.setPen(Qt::NoPen);
	painter.drawPixmap(0, 0, window);
	
	painter.setFont(QFont("DejaVu Sans", 16, QFont::Bold));
	painter.setPen(QPen(QColor(0, 0, 0)));
	painter.drawText(160, 100, "X2Play");
}

void X2Play::searchTextChanged(const QString &text) {
	if (text.size() > 0) conn->searchText(text);
	else resultBox->clear();
}

void X2Play::updateMatches(QList<QStringList> &matches) {
	resultBox->clear();
	for (int i=0; i<matches.size(); i++) {
		resultBox->addTopLevelItem(new QTreeWidgetItem(matches[i]));
	}
	resultBox->setCurrentItem(resultBox->topLevelItem(0));
}

void X2Play::playClicked(bool queue) {
	int id;
	conn->wait();
	if (!resultBox->currentItem()) return;
	if ((id = resultBox->currentItem()->text(3).toInt())) 
		conn->playId((unsigned int)id, queue);
	else
		conn->doCommand(resultBox->currentItem()->text(3));
	cancelClicked();
}

void X2Play::queueClicked() {
	playClicked(true);
}

void X2Play::cancelClicked() {
	//app->quit();
	hide();
}

QTreeWidget* X2Play::getResultBox() {return resultBox;}



/***************************************************************/
/* SearchLineEdit                                              */
/***************************************************************/

SearchLineEdit::SearchLineEdit(QWidget *parent) : QLineEdit(parent) {}

void SearchLineEdit::keyPressEvent(QKeyEvent *event) {
	if (event->key() == Qt::Key_Down) {
		QTreeWidgetItem *next = ((X2Play*)parentWidget())->getResultBox()->itemBelow(((X2Play*)parentWidget())->getResultBox()->currentItem());
		if (next) ((X2Play*)parentWidget())->getResultBox()->setCurrentItem(next);
	} else if (event->key() == Qt::Key_Up) {
		QTreeWidgetItem *next = ((X2Play*)parentWidget())->getResultBox()->itemAbove(((X2Play*)parentWidget())->getResultBox()->currentItem());
		if (next) ((X2Play*)parentWidget())->getResultBox()->setCurrentItem(next);
	} else if (event->key() == Qt::Key_Escape) {
		((X2Play*)parentWidget())->cancelClicked();
	} else {
		this->QLineEdit::keyPressEvent(event);
	}
}

#endif
